package org.jpass.dc.view.project;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.jpass.dc.business.project.logic.ProjectService;
import org.jpass.dc.business.user.bean.UserVOBean;
import org.jpass.dc.common.bean.TreeNode;
import org.jpass.dc.common.util.StringUtil;
import org.jpass.dc.view.common.BasicAction;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GetProjectListAction extends BasicAction {

	private static final long serialVersionUID = -7895282173758283916L;
	
	private Log log = LogFactory.getLog(getClass());
	
	private ProjectService projectService = new ProjectService();
	
	/* 树形菜单的节点id  */
	private String node;
	
	/* 树形菜单的节点名称  */
	private String parentPath;
	
	/* 树形菜单的深度  */
	private int depth;

	public String getNode() {
		return node;
	}

	public void setNode(String node) {
		this.node = node;
	}

	public String getParentPath() {
		return parentPath;
	}

	public void setParentPath(String parentPath) {
		this.parentPath = parentPath;
	}

	public int getDepth() {
		return depth;
	}

	public void setDepth(int depth) {
		this.depth = depth;
	}
	
	@Override
	public String execute() throws Exception {
		
		UserVOBean user = this.getCurrentUser();
		//System.out.println(user);
		//System.out.println(user.getId());
		List<TreeNode> projectList = new ArrayList<TreeNode>();
		Gson gson= new GsonBuilder().create();
		if (depth == 0) {
			projectList = projectService.getList(user);
		} else {
			if (!StringUtil.isBlank(parentPath)) {
				String path = this.getUserRealWrokRootPath();
				projectList = projectService.getChildrenList(path + "/" + parentPath);
			}
		}
		try {
			String json = gson.toJson(projectList);
			ServletActionContext.getResponse().setCharacterEncoding("utf-8");
			ServletActionContext.getResponse().getWriter().print(json);
		} catch (IOException e) {
			log.error(user.getEmail() + "请求用户信息获取PrintWriter时抛出异常，异常信息：\n", e);
		}
		return null;
	}

}
